package whisper.service.service.impl;

import com.google.common.io.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import whisper.common.support.DatePattern;
import whisper.common.utils.DateUtils;
import whisper.common.utils.FileUtils;
import whisper.service.constants.MediaInfo;
import whisper.service.constants.TaskStatus;
import whisper.service.domain.db.SourceMedia;
import whisper.service.domain.db.TargetMedia;
import whisper.service.domain.db.Task;
import whisper.service.repository.SourceMediaRepository;
import whisper.service.repository.TargetMediaRepository;
import whisper.service.repository.TaskRepository;
import whisper.service.service.MediaService;
import whisper.service.utils.PathUtils;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by JT on 2017/11/4.
 */
@Service
public class MediaServiceImpl implements MediaService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MediaInfo mediaInfo;

    @Autowired
    private SourceMediaRepository sourceMediaRepository;

    @Autowired
    private TargetMediaRepository targetMediaRepository;

    @Autowired
    private TaskRepository taskRepository;

    /**
     * 接收文件
     *
     * @param file
     */
    @Override
    public void receiveFile(MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();
        String sourceFileType = Files.getFileExtension(file.getOriginalFilename());
        String sourceFilename = PathUtils.getNewFilename(MediaInfo.MEDIA_FILENAME_PREFIX_UPLOAD, sourceFileType);
        String sourcePath = String.format("%s/%s", PathUtils.createUploadBasePath(), sourceFilename);
        String sourceFileSize = FileUtils.transformFilesizeToString(file.getSize());
        SourceMedia sourceMedia = new SourceMedia(originalFilename, sourceFilename, sourcePath, sourceFileSize,sourceFileType);

        // 接收文件
        String fullpath = mediaInfo.getMediaParentUploadPath() + sourcePath;
        FileUtils.createParentDirs(fullpath);
        // 使用流传输文件，使用Bytes传输会内存溢出
        FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(fullpath));
        // FileUtils.writeFileUsingBytes(fullpath, file.getBytes());
        logger.info("文件上传成功！文件路径[{}]", fullpath);

        long sourceId = sourceMediaRepository.saveSourceMedia(sourceMedia);

        String targetFileName = PathUtils.getNewFilename(MediaInfo.MEDIA_FILENAME_PREFIX_TARGET, "mp4");
        String targetFilePath = String.format("%s/%s", PathUtils.createUploadBasePath(), targetFileName);
        TargetMedia targetMedia = new TargetMedia(targetFileName, targetFilePath);
        long targetId = targetMediaRepository.saveTargetMedia(targetMedia);

        String taskRegisterTime = DateUtils.getNowFormat(DatePattern.DATE_PATTERN_STANDARD);
        Task task = new Task(sourceId, targetId, taskRegisterTime, TaskStatus.WAIT.getCode());
        taskRepository.addTask(task);
    }
}
